package com.example.remotecontrol.data.model

/**
 * 遥控代码库
 * 提供多种设备类型的红外编码
 */
object RemoteCodeLibrary {

    /**
     * 获取指定品牌和设备类型的地址码
     */
    fun getAddressCode(brand: String, deviceType: DeviceType): Int {
        return when (deviceType) {
            DeviceType.TV -> getTVAddressCode(brand)
            DeviceType.AC -> getACAddressCode(brand)
            DeviceType.AUDIO -> getAudioAddressCode(brand)
            DeviceType.PROJECTOR -> getProjectorAddressCode(brand)
            else -> 0
        }
    }

    /**
     * 获取指定品牌和设备类型的命令映射
     */
    fun getCommandMap(brand: String, deviceType: DeviceType): Map<String, Int> {
        return when (deviceType) {
            DeviceType.TV -> getTVCommandMap(brand)
            DeviceType.AC -> getACCommandMap(brand)
            DeviceType.AUDIO -> getAudioCommandMap(brand)
            DeviceType.PROJECTOR -> getProjectorCommandMap(brand)
            else -> emptyMap()
        }
    }

    /**
     * 电视遥控器地址码
     */
    private fun getTVAddressCode(brand: String): Int {
        return when (brand.lowercase()) {
            "samsung" -> 0x0707
            "lg" -> 0x20DF
            "sony" -> 0x010C
            "tcl" -> 0x3434
            "philips" -> 0x0202
            "panasonic" -> 0x4004
            "hisense" -> 0x1212
            "haier" -> 0x4040
            "skyworth" -> 0x4CB3
            "sharp" -> 0x5743
            "xiaomi" -> 0x1313
            "vizio" -> 0x0909
            "toshiba" -> 0x4545
            else -> 0x0707  // 默认使用Samsung码
        }
    }

    /**
     * 空调遥控器地址码
     */
    private fun getACAddressCode(brand: String): Int {
        return when (brand.lowercase()) {
            "gree" -> 0x4040
            "midea" -> 0xB24D
            "daikin" -> 0x1111
            "carrier" -> 0x2020
            "haier" -> 0x0505
            "lg" -> 0x8800
            "panasonic" -> 0x4004
            "toshiba" -> 0x4545
            "hitachi" -> 0x0303
            "aux" -> 0x4242
            "whirlpool" -> 0x7878
            "samsung" -> 0x0707
            else -> 0x4040  // 默认使用Gree码
        }
    }

    /**
     * 音响遥控器地址码
     */
    private fun getAudioAddressCode(brand: String): Int {
        return when (brand.lowercase()) {
            "sony" -> 0x1010
            "yamaha" -> 0x7A85
            "pioneer" -> 0xA55A
            "jbl" -> 0x0C0C
            "bose" -> 0x5050
            "denon" -> 0x2A2A
            "samsung" -> 0x0707
            "lg" -> 0x20DF
            "onkyo" -> 0x6060
            "harman" -> 0x0D0D
            else -> 0x1010  // 默认使用Sony码
        }
    }

    /**
     * 投影仪遥控器地址码
     */
    private fun getProjectorAddressCode(brand: String): Int {
        return when (brand.lowercase()) {
            "epson" -> 0x6363
            "benq" -> 0x4E4E
            "optoma" -> 0x4D4D
            "viewsonic" -> 0x6565
            "sony" -> 0x010C
            "panasonic" -> 0x4004
            "lg" -> 0x20DF
            "acer" -> 0x5757
            "xiaomi" -> 0x1313
            else -> 0x6363  // 默认使用Epson码
        }
    }

    /**
     * 电视遥控器命令映射
     */
    private fun getTVCommandMap(brand: String): Map<String, Int> {
        val commonTVCommands = mapOf(
            "POWER" to 0x40,
            "VOLUME_UP" to 0x41,
            "VOLUME_DOWN" to 0x42,
            "CHANNEL_UP" to 0x43,
            "CHANNEL_DOWN" to 0x44,
            "MUTE" to 0x45,
            "MENU" to 0x46,
            "HOME" to 0x47,
            "BACK" to 0x48,
            "OK" to 0x49,
            "UP" to 0x50,
            "DOWN" to 0x51,
            "LEFT" to 0x52,
            "RIGHT" to 0x53,
            "INPUT" to 0x54,
            "EXIT" to 0x55,
            "NUMBER_0" to 0x60,
            "NUMBER_1" to 0x61,
            "NUMBER_2" to 0x62,
            "NUMBER_3" to 0x63,
            "NUMBER_4" to 0x64,
            "NUMBER_5" to 0x65,
            "NUMBER_6" to 0x66,
            "NUMBER_7" to 0x67,
            "NUMBER_8" to 0x68,
            "NUMBER_9" to 0x69,
            "INFO" to 0x70,
            "GUIDE" to 0x71,
            "RED" to 0x72,
            "GREEN" to 0x73,
            "YELLOW" to 0x74,
            "BLUE" to 0x75,
            "SOURCE" to 0x54  // Same as INPUT
        )

        // 针对特定品牌的自定义命令
        return when (brand.lowercase()) {
            "samsung" -> commonTVCommands + mapOf(
                "TOOLS" to 0x76,
                "SMART_HUB" to 0x77,
                "E_MANUAL" to 0x78
            )
            "lg" -> commonTVCommands + mapOf(
                "SIMPLINK" to 0x76,
                "LIVE_ZOOM" to 0x77,
                "QUICK_MENU" to 0x78
            )
            "sony" -> commonTVCommands + mapOf(
                "SYNC_MENU" to 0x76,
                "DISCOVER" to 0x77,
                "NETFLIX" to 0x78
            )
            "xiaomi" -> commonTVCommands + mapOf(
                "NETFLIX" to 0x76,
                "YOUTUBE" to 0x77,
                "VOICE" to 0x78
            )
            else -> commonTVCommands
        }
    }

    /**
     * 空调遥控器命令映射
     */
    private fun getACCommandMap(brand: String): Map<String, Int> {
        val commonACCommands = mapOf(
            "POWER" to 0x40,
            "TEMP_UP" to 0x41,
            "TEMP_DOWN" to 0x42,
            "MODE" to 0x43,
            "FAN" to 0x44,
            "SWING" to 0x45,
            "TURBO" to 0x46,
            "SLEEP" to 0x47,
            "TIMER" to 0x48,
            "ECO" to 0x49,
            "DISPLAY" to 0x50,
            "HEALTH" to 0x51,
            "SET_TEMP_16" to 0x60,
            "SET_TEMP_17" to 0x61,
            "SET_TEMP_18" to 0x62,
            "SET_TEMP_19" to 0x63,
            "SET_TEMP_20" to 0x64,
            "SET_TEMP_21" to 0x65,
            "SET_TEMP_22" to 0x66,
            "SET_TEMP_23" to 0x67,
            "SET_TEMP_24" to 0x68,
            "SET_TEMP_25" to 0x69,
            "SET_TEMP_26" to 0x6A,
            "SET_TEMP_27" to 0x6B,
            "SET_TEMP_28" to 0x6C,
            "SET_TEMP_29" to 0x6D,
            "SET_TEMP_30" to 0x6E,
            "MODE_AUTO" to 0x70,
            "MODE_COOL" to 0x71,
            "MODE_HEAT" to 0x72,
            "MODE_DRY" to 0x73,
            "MODE_FAN" to 0x74,
            "FAN_AUTO" to 0x75,
            "FAN_LOW" to 0x76,
            "FAN_MID" to 0x77,
            "FAN_HIGH" to 0x78
        )

        // 针对特定品牌的自定义命令
        return when (brand.lowercase()) {
            "gree" -> commonACCommands + mapOf(
                "SELF_CLEAN" to 0x79,
                "QUIET" to 0x7A
            )
            "midea" -> commonACCommands + mapOf(
                "FOLLOW_ME" to 0x79,
                "COMFORT" to 0x7A
            )
            "daikin" -> commonACCommands + mapOf(
                "COMFORT" to 0x79,
                "POWERFUL" to 0x7A,
                "ECONO" to 0x7B
            )
            else -> commonACCommands
        }
    }

    /**
     * 音响遥控器命令映射
     */
    private fun getAudioCommandMap(brand: String): Map<String, Int> {
        val commonAudioCommands = mapOf(
            "POWER" to 0x40,
            "VOLUME_UP" to 0x41,
            "VOLUME_DOWN" to 0x42,
            "MUTE" to 0x43,
            "INPUT" to 0x44,
            "PLAY" to 0x45,
            "PAUSE" to 0x46,
            "STOP" to 0x47,
            "NEXT" to 0x48,
            "PREV" to 0x49,
            "REPEAT" to 0x50,
            "SHUFFLE" to 0x51,
            "EQ" to 0x52,
            "BASS_UP" to 0x53,
            "BASS_DOWN" to 0x54,
            "TREBLE_UP" to 0x55,
            "TREBLE_DOWN" to 0x56,
            "SURROUND" to 0x57,
            "BLUETOOTH" to 0x58,
            "FM" to 0x59,
            "AM" to 0x5A,
            "AUX" to 0x5B,
            "USB" to 0x5C,
            "OPTICAL" to 0x5D,
            "HDMI" to 0x5E
        )

        // 针对特定品牌的自定义命令
        return when (brand.lowercase()) {
            "sony" -> commonAudioCommands + mapOf(
                "CLEAR_AUDIO" to 0x5F,
                "FOOTBALL" to 0x60
            )
            "yamaha" -> commonAudioCommands + mapOf(
                "SCENE1" to 0x5F,
                "SCENE2" to 0x60,
                "SCENE3" to 0x61,
                "SCENE4" to 0x62
            )
            "bose" -> commonAudioCommands + mapOf(
                "PRESET1" to 0x5F,
                "PRESET2" to 0x60,
                "PRESET3" to 0x61
            )
            else -> commonAudioCommands
        }
    }

    /**
     * 投影仪遥控器命令映射
     */
    private fun getProjectorCommandMap(brand: String): Map<String, Int> {
        val commonProjectorCommands = mapOf(
            "POWER" to 0x40,
            "POWER_ON" to 0x41,
            "POWER_OFF" to 0x42,
            "INPUT" to 0x43,
            "MENU" to 0x44,
            "EXIT" to 0x45,
            "UP" to 0x46,
            "DOWN" to 0x47,
            "LEFT" to 0x48,
            "RIGHT" to 0x49,
            "OK" to 0x4A,
            "VOLUME_UP" to 0x4B,
            "VOLUME_DOWN" to 0x4C,
            "MUTE" to 0x4D,
            "FREEZE" to 0x4E,
            "BLANK" to 0x4F,
            "AUTO" to 0x50,
            "KEYSTONE_UP" to 0x51,
            "KEYSTONE_DOWN" to 0x52,
            "ZOOM_IN" to 0x53,
            "ZOOM_OUT" to 0x54,
            "FOCUS_PLUS" to 0x55,
            "FOCUS_MINUS" to 0x56,
            "HDMI1" to 0x57,
            "HDMI2" to 0x58,
            "VGA" to 0x59,
            "USB" to 0x5A,
            "BRIGHTNESS_UP" to 0x5B,
            "BRIGHTNESS_DOWN" to 0x5C,
            "CONTRAST_UP" to 0x5D,
            "CONTRAST_DOWN" to 0x5E
        )

        // 针对特定品牌的自定义命令
        return when (brand.lowercase()) {
            "epson" -> commonProjectorCommands + mapOf(
                "COLOR_MODE" to 0x5F,
                "ASPECT" to 0x60,
                "USER" to 0x61
            )
            "benq" -> commonProjectorCommands + mapOf(
                "PRESET" to 0x5F,
                "DYNAMIC" to 0x60,
                "PRESENTATION" to 0x61
            )
            "optoma" -> commonProjectorCommands + mapOf(
                "3D" to 0x5F,
                "DISPLAY_MODE" to 0x60,
                "SLEEP_TIMER" to 0x61
            )
            else -> commonProjectorCommands
        }
    }

    /**
     * 电视品牌列表
     */
    val TV_BRANDS = listOf(
        "Samsung", "LG", "Sony", "TCL", "Philips", "Panasonic", "Hisense", 
        "Haier", "Skyworth", "Sharp", "Xiaomi", "Vizio", "Toshiba"
    )

    /**
     * 空调品牌列表
     */
    val AC_BRANDS = listOf(
        "Gree", "Midea", "Daikin", "Carrier", "Haier", "LG", "Panasonic", 
        "Toshiba", "Hitachi", "AUX", "Whirlpool", "Samsung"
    )

    /**
     * 音响品牌列表
     */
    val AUDIO_BRANDS = listOf(
        "Sony", "Yamaha", "Pioneer", "JBL", "Bose", "Denon", "Samsung", 
        "LG", "Onkyo", "Harman"
    )

    /**
     * 投影仪品牌列表
     */
    val PROJECTOR_BRANDS = listOf(
        "Epson", "BenQ", "Optoma", "ViewSonic", "Sony", "Panasonic", 
        "LG", "Acer", "Xiaomi"
    )
} 